﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
using System.Windows.Forms;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace Library1
{
    public class UpdateServiceTruyenNhan : UpdateApplication
    {
        public override string ClientDirectoryPath
        {
            get
            {
                if (base.ClientDirectoryPath == "")
                {
                    ModifyRegistry mr = new ModifyRegistry();
                    mr.BaseRegistryKey = Microsoft.Win32.Registry.LocalMachine;
                    mr.SubKey = "SYSTEM\\CurrentControlSet\\Services\\RMII Cuc - TruyenNhan";
                    string pathToExecuteFile = mr.Read("ImagePath");
                    //bỏ dấu " ở đầu chuỗi
                    string pathToFolder = pathToExecuteFile.Substring(1, pathToExecuteFile.LastIndexOf("\\") - 1);

                    return pathToFolder;
                }
                else
                {
                    return base.ClientDirectoryPath;
                }
            }
            set
            {
                base.ClientDirectoryPath = value;
            }
        }
       
        public override string ServerDirectoryName
        {
            get
            {
                if (base.ServerDirectoryName == "")
                {
                    iniForWinForm i = new iniForWinForm(Application.StartupPath + "\\WebUpdate.ini");
                    return i.IniReadValue("General", "Version");
                }
                else
                {
                    return base.ServerDirectoryName;
                }
            }
            set
            {
                base.ServerDirectoryName = value;
            }
        }        

        public UpdateServiceTruyenNhan()
        {                     
        }              

        public void DoAll()
        {
            //Threads t = new Threads(updates, Application.StartupPath, version, new OutputDelegate(UpdateText), new ProgressDelegate(UpdateProgress));
            //Thread thread1 = new Thread(new ThreadStart(t.Do));
            //thread1.Start();

            //1. Stop Service
            ServiceController serviceController = new ServiceController();
            serviceController.ServiceName = "RMII Cuc - TruyenNhan";
            if (serviceController.Status == ServiceControllerStatus.Running)
            {
                serviceController.Stop();
                serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
            }

            //2. Get service 's installation path and update
            Update();

            //3. Start service
            if (serviceController.Status == ServiceControllerStatus.Stopped)
            {
                serviceController.Start();
                serviceController.WaitForStatus(ServiceControllerStatus.Running);
            }
        } 
       
        public void Update()
        {
            Download();            
                        
            ServerDirectoryName = "Scripts";
            ClientDirectoryPath = ClientDirectoryPath + "\\Scripts";
            Download();
            UpdateApplication.RunScripts(ClientDirectoryPath);
            
            //wait because we will delete that file immediately
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            Directory.Delete(ClientDirectoryPath, true);
        }        
    }
}
